<?php

//数据库配置
$ip = "60.205.204.225";
$databasename = "BaiGongBao2";
$username = "root";
$password = "xx921031";

//model数组写控制器名 例如User,驼峰式
//$model[0] = "User";
//$model[1] = "UserMoney";

foreach ($model as $key => $value) {
	create_php($model[$key]);
	create_html($model[$key],$ip,$databasename, $username, $password);
}

//驼峰转下划线
function toUnderScore($str){
    $dstr = preg_replace_callback('/([A-Z]+)/',function($matchs){
        return '_'.strtolower($matchs[0]);
        },$str);
    return trim(preg_replace('/_{2,}/','_',$dstr),'_');
}


function create_php($name){
	$myfile = fopen("../App/Home/Controller/".$name."Controller.class.php", "w") or die("Unable to open file!");
	$php_text = file_get_contents("file/controller.php");
	$php_text = str_replace("_NAME_",$name,$php_text);
	fwrite($myfile, $php_text);
	fclose($myfile);
	echo $name."控制器创建成功<br>";
}

function create_html($name,$ip,$databasename, $username, $password){

	// 创建连接
	$conn = new mysqli($ip, $username, $password);
	 
	// 检测连接
	if ($conn->connect_error) {
	    die("连接失败: " . $conn->connect_error);
	} 

	$sql = "SELECT column_name as field,column_comment as title,'100' as width , 'text' as edit  from information_schema.columns where table_name='".toUnderScore(strtolower($name))."' and table_schema='".$databasename."'";
	$res = array();
	$result = $conn->query($sql);
	if ($result->num_rows > 0) {
	    // 输出数据
	    while($row = $result->fetch_assoc()) {
	    	array_push($res,$row);
	    }
	}
	$conn->close();

	$arr['title'] = "操作";
	$arr['fixed'] = "right";
	$arr['width'] = "120";
	$arr['align'] = "center";
	$arr['toolbar'] = "#barDemo";

	array_push($res, $arr);


	$res = json_encode($res,JSON_UNESCAPED_UNICODE);

	$dir = iconv("UTF-8", "GBK", "../App/Home/View/".$name);
    if (!file_exists($dir)){
        mkdir ($dir,0777,true);
        echo '创建文件夹'.$name.'成功<br>';
    }

	$myfile = fopen("../App/Home/View/".$name."/index.html", "w") or die("Unable to open file!");
	$html_text = file_get_contents("file/index.html");

	$html_text = str_replace("_COLS_",$res,$html_text);
	fwrite($myfile, $html_text);
	fclose($myfile);
	echo $name."/index.html创建成功<br>";
}


// 

// 

// 